/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiDadosFuncionario.java
 *
 * Created on 24/11/2011, 21:19:16
 */
package view.gui;

import javax.swing.JOptionPane;
import model.dominio.Funcionario;
import controller.ControllerFuncionario;

/**
 *
 * @author Leandro
 */
public class GuiDadosFuncionario  extends JInternalFramePermissao {

    private Funcionario funcionario;

    /** Creates new form GuiDadosFuncionario */
    public GuiDadosFuncionario() {
        initComponents();
        funcionario = new Funcionario();
    }
/**
     * Metodo onde armazena os dados digitado no programa
     * @param f Funcionario contendo filtros do cadastro 
     */
    public GuiDadosFuncionario(Funcionario f) {
        initComponents();

        boxNome.setText(f.getNome());
        boxCpf.setText(f.getCpf());
        boxCargo.setText(f.getCargo());
        boxFone.setText(f.getTelefone());
        boxEnd.setText(f.getEndereco());
        jButton1.setText("Editar");
        funcionario = f;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        boxNome = new javax.swing.JTextField();
        boxCargo = new javax.swing.JTextField();
        boxEnd = new javax.swing.JTextField();
        boxCpf = new javax.swing.JFormattedTextField();
        boxFone = new javax.swing.JFormattedTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Cadastro do Funcionário");
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Funcionário"));
        jPanel1.setLayout(null);

        jLabel1.setText("Nome*:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 30, 50, 30);

        jLabel2.setText("CPF*:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 70, 40, 30);

        jLabel3.setText("Endereço: ");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(10, 110, 70, 30);

        jLabel4.setText("Telefone:");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(10, 150, 60, 30);

        jLabel5.setText("Cargo*:");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(190, 70, 50, 30);

        boxNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxNomeActionPerformed(evt);
            }
        });
        jPanel1.add(boxNome);
        boxNome.setBounds(70, 30, 300, 30);

        boxCargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxCargoActionPerformed(evt);
            }
        });
        jPanel1.add(boxCargo);
        boxCargo.setBounds(240, 70, 130, 30);

        boxEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxEndActionPerformed(evt);
            }
        });
        jPanel1.add(boxEnd);
        boxEnd.setBounds(70, 110, 300, 30);

        try {
            boxCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        boxCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxCpfActionPerformed(evt);
            }
        });
        jPanel1.add(boxCpf);
        boxCpf.setBounds(70, 70, 110, 30);

        try {
            boxFone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        boxFone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxFoneActionPerformed(evt);
            }
        });
        jPanel1.add(boxFone);
        boxFone.setBounds(70, 150, 140, 30);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 10, 390, 210);
        jPanel1.getAccessibleContext().setAccessibleName(null);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/salvar.png"))); // NOI18N
        jButton1.setText("Cadastrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(30, 240, 140, 60);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/cancelar.png"))); // NOI18N
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(210, 240, 140, 60);

        setBounds(0, 0, 419, 346);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed
/**
     * Metodo para ler o que foi digitado nos campos,verificar se os campos
     * obrigatorios foram preenchidos e dizer se os dados do funcionario foi ou
     * nao salvos com sucesso.
     * @param evt 
     */
    public void guardar() {
        if (boxNome.getText().isEmpty() || boxCpf.getText().isEmpty() || boxCargo.getText().length() < 4) { // TODO errado o segundo isEmpty
            JOptionPane.showMessageDialog(null, "Campo(s) obrigatórios não foram preenchidos!", "ERRO", JOptionPane.ERROR_MESSAGE);
            return;
        }
        funcionario.setNome(boxNome.getText());
        funcionario.setEndereco(boxEnd.getText());
        funcionario.setCpf(boxCpf.getText());
        funcionario.setTelefone(boxFone.getText());
        funcionario.setCargo(boxCargo.getText());
        if (ControllerFuncionario.salvarFuncionario(funcionario)) {
            JOptionPane.showMessageDialog(null, "Dados salvos com sucesso!", "Salvar Funcionario", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "Erro ao salvar dados!", "Salvar Funcionario", JOptionPane.ERROR_MESSAGE);
        }
        this.dispose();

    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        guardar();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void boxNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxNomeActionPerformed
        guardar();        // TODO add your handling code here:
    }//GEN-LAST:event_boxNomeActionPerformed

    private void boxCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxCpfActionPerformed
        guardar();
    }//GEN-LAST:event_boxCpfActionPerformed

    private void boxCargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxCargoActionPerformed
        guardar();
    }//GEN-LAST:event_boxCargoActionPerformed

    private void boxEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxEndActionPerformed
        guardar();
    }//GEN-LAST:event_boxEndActionPerformed

    private void boxFoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxFoneActionPerformed
        guardar();
    }//GEN-LAST:event_boxFoneActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField boxCargo;
    private javax.swing.JFormattedTextField boxCpf;
    private javax.swing.JTextField boxEnd;
    private javax.swing.JFormattedTextField boxFone;
    private javax.swing.JTextField boxNome;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public int getPermissao() {
        if(funcionario.getCodigo()!=null && funcionario.getCodigo()>0){
            return 10;
        }else{
            return 9;
        }
    }
}
